function fitTarget(argument) {
	var selectedItems = app.activeDocument.selection;
	var option = argument.optionGroup
	var align = argument.alignGroup
	var ratio = parseFloat(option.fitRatio.val) || 1

	if ( selectedItems.length === 1 ) {
		var source = selectedItems[0]

		option.fitWidth.on && !option.fitHeight.on && fitCanvasWidth(source, ratio)
		option.fitHeight.on && !option.fitWidth.on && fitCanvasHeight(source, ratio)
		option.fitWidth.on && option.fitHeight.on && fitCanvasWidthHeight(source, ratio)

		align.fitAlignLeft.on && alignCanvasLeft(source)
		align.fitAlignCenter.on && alignCanvasCenter(source)
		align.fitAlignRight.on && alignCanvasRight(source)
		align.fitAlignTop.on && alignCanvasTop(source)
		align.fitAlignMiddle.on && alignCanvasMiddle(source)
		align.fitAlignBottom.on && alignCanvasBottom(source)
	}

	if ( selectedItems.length > 1 ) {
		var source = option.fitSwap.on ? selectedItems[1] : selectedItems[0]
		var target = option.fitSwap.on ? selectedItems[0] : selectedItems[1]

		option.fitWidth.on && !option.fitHeight.on && fitWidth(source, target, ratio)
		option.fitHeight.on && !option.fitWidth.on && fitHeight(source, target, ratio)
		option.fitWidth.on && option.fitHeight.on && fitWidthHeight(source, target, ratio)

		align.fitAlignLeft.on && alignLeft(source, target)
		align.fitAlignCenter.on && alignCenter(source, target)
		align.fitAlignRight.on && alignRight(source, target)
		align.fitAlignTop.on && alignTop(source, target)
		align.fitAlignMiddle.on && alignMiddle(source, target)
		align.fitAlignBottom.on && alignBottom(source, target)

		option.fitRemove.on && target.remove()
	}
}

function getCanvasSize() {
	var CURR_DOC = app.activeDocument;
	var CURR_ARTBOARD_INDEX = CURR_DOC.artboards.getActiveArtboardIndex()
	var canvas = CURR_DOC.artboards[CURR_ARTBOARD_INDEX].artboardRect

	function _getArtboardSize(canvas) {
		var rect = canvas.toString().split(',')
		var x1 = +rect[0]
		var y1 = +rect[1]
		var x2 = +rect[2]
		var y2 = +rect[3]
		var rectangle = {
			y1: y1,
			x1: x1,
			y2: y2,
			x2: x2,
			width: Math.abs(x2 - x1),
			height: Math.abs(y2 - y1)
		}
		return rectangle
	}
	return _getArtboardSize(canvas)
}



function fitCanvasWidth(source, ratio) {
	var size = getCanvasSize()
	var sourceRatio = source.width / source.height
	source.width = size.width * ratio
	source.height = source.width / sourceRatio
}

function fitCanvasHeight(source, ratio) {
	var size = getCanvasSize()
	var sourceRatio = source.width / source.height
	source.height = size.height * ratio
	source.width = source.height * sourceRatio
}

function fitCanvasWidthHeight(source, ratio) {
	var size = getCanvasSize()
	var sourceRatio = source.width / source.height
	source.height = size.height * ratio
	source.width = size.width * ratio
}

function alignCanvasLeft(source) {
	var size = getCanvasSize()
	source.left = size.x1
}

function alignCanvasCenter(source) {
 	var size = getCanvasSize()
	var gap = source.width - size.width
	source.left = size.x1 - gap / 2
}

function alignCanvasRight(source) {
 	var size = getCanvasSize()
	var gap = source.width - size.width
	source.left = size.x1 - gap
}

function alignCanvasTop(source) {
	var size = getCanvasSize()
	source.top = size.y1
}

function alignCanvasMiddle(source) {
 	var size = getCanvasSize()
	var gap = source.height - size.height
	source.top = size.y1 + gap / 2
}

function alignCanvasBottom(source) {
 	var size = getCanvasSize()
	source.top = size.y2 + source.height
}


// =================== two object =================

function fitWidth(source, target, ratio) {
	var sourceRatio = source.width / source.height
	source.width = target.width * ratio
	source.height = source.width  / sourceRatio
}

function fitHeight(source, target, ratio) {
	var sourceRatio = source.width / source.height
	source.height = target.height * ratio
	source.width = source.height * sourceRatio
}

function fitWidthHeight(source, target, ratio) {
	source.height = target.height * ratio
	source.width = target.width * ratio
}

function alignLeft(source, target) {
	source.left = target.left
}

function alignCenter(source, target) {
  var gap = (source.width - target.width) / 2
	source.left = target.left - gap
}

function alignRight(source, target) {
  var gap = source.width - target.width
	source.left = target.left - gap
}

function alignTop(source, target) {
	source.top = target.top
}

function alignMiddle(source, target) {
  var gap = (source.height - target.height) / 2
	source.top = target.top + gap
}

function alignBottom(source, target) {
  var gap = source.height - target.height
	source.top = target.top + gap
}

